<%@page import="qldc.pm.tienan.constants.TrangThaiKetQuaTrichLucEnum"%>
<%@page import="com.liferay.portal.kernel.language.LanguageUtil"%>
<%@page import="qldc.pm.tienan.service.TienAnLocalServiceUtil"%>
<%@page import="java.util.ArrayList"%>
<%@page import="qldc.pm.tienan.model.TienAn"%>
<%@page import="qldc.pm.tienan.service.KetQuaTrichLucLocalServiceUtil"%>
<%@page import="qldc.pm.tienan.model.KetQuaTrichLuc"%>
<%@page import="qldc.pm.qtht.dmdc.enumclass.EnumGioiTinh"%>
<%@page import="qldc.pm.qtht.dmdc.service.DanTocLocalServiceUtil"%>
<%@page import="qldc.pm.qtht.dmdc.model.DanToc"%>
<%@page import="qldc.pm.csdl.cd90.model.CMND"%>
<%@page import="qldc.pm.csdl.cd90.service.CMNDLocalServiceUtil"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="qldc.pm.tienan.service.YeuCauTrichLucLocalServiceUtil"%>
<%@page import="qldc.pm.tienan.model.YeuCauTrichLuc"%>
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="qldc.pm.qtht.dmdc.service.TinhLocalServiceUtil"%>
<%@page import="qldc.pm.qtht.dmdc.model.Tinh"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.kernel.util.Constants"%>
<%@include file="../../init.jsp"%>

<portlet:resourceURL var="layMaKetQuaYCTL" id="layMaKetQuaYCTL">
</portlet:resourceURL>

<%
String redirect = ParamUtil.getString(request, "redirect");
String pathThemeImages = themeDisplay.getPathThemeImages();

long yeuCauTrichLucId = ParamUtil.getLong(request, "yeuCauTrichLucId");
long ketQuaTrichLucId = 0L;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String mode = "add";
String soDinhDanhCaNhan =ParamUtil.getString(request, "soDinhDanhCaNhan");
YeuCauTrichLuc yeuCauTrichLuc = YeuCauTrichLucLocalServiceUtil.findByYeuCauTrichLucId(yeuCauTrichLucId);
String maKetQuaTrichLuc = "";
String kinhGui = "";
String maPhieuTrichLuc = "";
String ngayGuiYeuCauTrichLuc = "";
String coQuanYeuCauTrichLuc = "";
String hoVaTenNguoiCanTrichLuc = "";
String tinhTrangAnTich = "";
String thongTinAnTich = "";
String toiDanh = "";
String hinhPhatChinh = "";
String hinhPhatBoSung = "";
String tinhTrangThiHanhAn = "";
int ketQuaSauKhiTrichLuc = 0;
String nguoiTraCuu = "";
String ngayTraCuu = sdf.format(new Date());
long doiTuongTienAnId = ParamUtil.getLong(request, "doiTuongTienAnId");

List<TienAn> anTichs = new ArrayList<TienAn>();

if(Validator.isNotNull(yeuCauTrichLuc)) {
	maPhieuTrichLuc = yeuCauTrichLuc.getMaYeuCauTrichLuc();
	ngayGuiYeuCauTrichLuc = Validator.isNotNull(yeuCauTrichLuc.getNgayLapTrichLuc())? sdf.format(yeuCauTrichLuc.getNgayLapTrichLuc()): "";
	coQuanYeuCauTrichLuc = yeuCauTrichLuc.getCoQuanYeuCauTrichLuc();
	hoVaTenNguoiCanTrichLuc = yeuCauTrichLuc.getHoVaTenDoiTuongTrichLuc();

	if (doiTuongTienAnId > 0){
		anTichs =  TienAnLocalServiceUtil.findByDoiTuongTienAnId(doiTuongTienAnId);
	}
	KetQuaTrichLuc ketQuaTrichLuc = KetQuaTrichLucLocalServiceUtil.findByYeuCauTrichLucId(yeuCauTrichLucId);
	
	if (Validator.isNotNull(ketQuaTrichLuc)){
		ketQuaTrichLucId = ketQuaTrichLuc.getKetQuaTrichLucId();
		mode = "edit";		
		maKetQuaTrichLuc = ketQuaTrichLuc.getMaKetQuaTrichLuc();
		kinhGui = ketQuaTrichLuc.getKinhGui();
		tinhTrangAnTich = ketQuaTrichLuc.getTinhTrangAnTich();
		thongTinAnTich = ketQuaTrichLuc.getThongTinAnTich();
		toiDanh = ketQuaTrichLuc.getToiDanh();
		hinhPhatChinh = ketQuaTrichLuc.getHinhPhatChinh();
		hinhPhatBoSung = ketQuaTrichLuc.getHinhPhatBoSung();
		tinhTrangThiHanhAn = ketQuaTrichLuc.getTrinhTrangThiHanhAn();
		ketQuaSauKhiTrichLuc = ketQuaTrichLuc.getKetQua();
		nguoiTraCuu = ketQuaTrichLuc.getNguoiTraCuuHoSo();
		ngayTraCuu = Validator.isNotNull(ketQuaTrichLuc.getNgayTraCuu()) ? sdf.format(ketQuaTrichLuc.getNgayTraCuu()) :sdf.format(new Date());
	}
	else {
		ketQuaSauKhiTrichLuc = TrangThaiKetQuaTrichLucEnum.KHONG_CO_TIEN_AN.getValue();
		boolean daXoaAnTich =true;
		int idx = 1;
		for (TienAn ts : anTichs){
			thongTinAnTich += idx+". "+ts.getBanAnSo()+",  "+(Validator.isNotNull(ts.getNgayToaAn())? sdf.format(ts.getNgayToaAn()) :"") +", "+ts.getToaAnXuPhat();
			thongTinAnTich+="\n";
			toiDanh+= idx+". "+ts.getToiDanh()+"\n";
			hinhPhatChinh +=idx+". "+ts.getHinhPhatChinh()+"\n";
			hinhPhatBoSung+=idx+". "+ts.getHinhPhatBoSung()+"\n";
			if (ts.getXoaAnTich()){
				daXoaAnTich = false;
			}
			idx++;
		}
		if (Validator.isNotNull(portletConfig) && Validator.isNotNull(actionRequest) ){
			if (daXoaAnTich && anTichs.size() > 0){
				ketQuaSauKhiTrichLuc = TrangThaiKetQuaTrichLucEnum.DA_XOA_AN_TICH.getValue();
				tinhTrangThiHanhAn = LanguageUtil.get(portletConfig, actionRequest.getLocale(), "da-xoa-an-tich");
			} else if (!daXoaAnTich && anTichs.size() > 0){
				ketQuaSauKhiTrichLuc = TrangThaiKetQuaTrichLucEnum.DANG_THI_HANH_AN.getValue();
				tinhTrangThiHanhAn = LanguageUtil.get(portletConfig, actionRequest.getLocale(), "dang-thi-hanh-an");
	
			}
		}

	}
}

%>

<portlet:actionURL var="capNhatKetQuaTrichLucTienAnActionURL">
	<portlet:param name="mvcPath" value="/html/view.jsp"/>
	<portlet:param name="chonChucNangItem" value="<%=TienAnConstants.QUAN_LY_YEU_CAU_TRICH_LUC %>"/>
	<portlet:param name="<%=TienAnConstants.ACTION_HANDLER_PARAM %>" value="<%=TienAnConstants.QUAN_LY_YEU_CAU_TRICH_LUC %>"/>
	<portlet:param name="<%=Constants.ACTION %>" value="capNhatKetQuaTrichLucTienAn"/>
	<portlet:param name="redirect" value="<%=redirect %>"/>
	<portlet:param name="yeuCauTrichLucId" value="<%=String.valueOf(yeuCauTrichLucId) %>"/>
</portlet:actionURL>

<script type="text/javascript">
$(function() {
	
	$("#<portlet:namespace/>ngayTraCuu").datepicker({
		showOn: "both",
		buttonImage: "<%=pathThemeImages%>/common/calendar.png",
		buttonImageOnly: true,
		buttonText: "<liferay-ui:message key="ngay-tra-cuu" />",
		dateFormat: "dd/mm/yy"
	});

});

</script>


<script type="text/javascript">

function <portlet:namespace/>validationForm(){
	var isOk = true;
	
	var maso = $('#<portlet:namespace/>maKetQuaTrichLuc');
	if (maso.val() == ''){
		showPopupConfirm('<liferay-ui:message key="xin-vui-long-lay-ma-ket-qua-trich-luc" />',['Ok'],function(index){
			maso.focus();
		});
		isOk = false;
	}
	else{
		var inputList = $(".mandatory");		
		if (inputList.length > 0){
			for (var i = 0;i<inputList.length;i++){
				var firstItem = inputList.eq(i);
				if (firstItem.val() == ''){
					var messageName = firstItem.attr('message').toString();
					showPopupConfirm('<liferay-ui:message key="xin-vui-long-nhap-thong-tin" />  : ['+messageName+']',['Ok'],function(index){
						$(firstItem).focus();
						isOk = false;
						return;
					});
					isOk = false;
					break;
				}
			}	
		}
		
	}
	return isOk;
	
}

function <portlet:namespace/>luuKetQuaTrichLuc() {
	$('#<portlet:namespace/>fm').removeAttr("target");
	$('#<portlet:namespace/>cmd').val('<%=TienAnConstants.LUU%>');
	var co = '<liferay-ui:message key="co" />';
 	var khong = '<liferay-ui:message key="khong" />';
	if(<portlet:namespace/>validationForm()){
		showPopupConfirm('<liferay-ui:message key="ban-co-muon-luu-thong-tin" />',[co,khong],function(index){
			if (index == 0){
				$("#<portlet:namespace/>fm").submit();
			}
		});	
	} 
}; 

function <portlet:namespace/>inKetQuaTrichLuc() {
	if(<portlet:namespace/>validationForm()){
		$('#<portlet:namespace/>cmd').val('<%=TienAnConstants.IN%>');
		$("#<portlet:namespace/>fm").attr('target', '_blank');
		$("#<portlet:namespace/>fm").submit();		
	} 
}; 

function layMa() {
	jQuery.ajax({
		url : "<%=layMaKetQuaYCTL.toString()%>",
		dataType : 'json',
		type : "POST",
		cache : false,
		timeout : 10000,
		async : false,
		success : function(data) {
			$("#<portlet:namespace/>maKetQuaTrichLuc").val(data.maKetQuaTrichLuc);			
		},
		error : function(xhr, textStatus, errorThrown) {
			alert(textStatus);
		}
	});
	
}

</script>

<liferay-ui:header title="ket-qua-xac-minh-thong-tin-ly-lich-tu-phap" showBackURL="<%=false %>"></liferay-ui:header>

<form name="<portlet:namespace />fm"  id="<portlet:namespace />fm" action="<%=capNhatKetQuaTrichLucTienAnActionURL.toString() %>"  method="POST" target="_blank" >
	<input type="hidden" name="<portlet:namespace/>yeuCauTrichLucId" value="<%=yeuCauTrichLucId%>">
	<input type="hidden" name="<portlet:namespace/>ketQuaTrichLucId" value="<%=ketQuaTrichLucId%>">
			<aui:input name="<%= Constants.CMD %>" type="hidden" />
	<input type="hidden" name = "<portlet:namespace/>soDinhDanhCaNhan" value="<%=soDinhDanhCaNhan%>"/>
	
		<input type="hidden" name = "<portlet:namespace/>ketQuaSauKhiTrichLuc" value="<%=ketQuaSauKhiTrichLuc%>"/>
	
	<table class="gridnormal">
		<tr>
					<td class="aui-w20"><liferay-ui:message key="ma-ket-qua-trich-luc" />&nbsp;<font color="red">*</font></td>
					<td class="aui-w30">
						<input type="text" required id="<portlet:namespace/>maKetQuaTrichLuc" name="<portlet:namespace/>maKetQuaTrichLuc" style="width: 50%" readonly="readonly" value="<%=maKetQuaTrichLuc%>">
						<c:if test='<%= mode.equalsIgnoreCase("add") %>'>
						<input type="button" class="btn" id="<portlet:namespace/>layMaKetQuaTrichLuc" value="<liferay-ui:message key="lay-so" />" onclick="layMa();"/>
						</c:if>
					</td>
			
		</tr>
			<tr>
			<td><liferay-ui:message key="kinh-gui" />&nbsp;<font color="red">*</font></td>
			<td colspan="3"><input type="text"  class="mandatory" message="<liferay-ui:message key='kinh-gui' />"  style="width: 96%" name="<portlet:namespace />kinhGui" id="<portlet:namespace />kinhGui" value="<%=kinhGui%>" /></td>

			</tr>
		<tr>
			<td><liferay-ui:message key="ma-phieu-xac-minh" /></td>
			<td><input type="text" name="<portlet:namespace />maPhieuTrichLuc" id="<portlet:namespace />maPhieuTrichLuc" readonly="readonly" value="<%=maPhieuTrichLuc%>" /></td>

			<td><liferay-ui:message key="ngay-gui-yeu-cau" /></td>
			<td><input type="text" name="<portlet:namespace />ngayGuiYeuCau"  style="width: 92%" id="<portlet:namespace />ngayGuiYeuCau" readonly="readonly" value="<%=ngayGuiYeuCauTrichLuc%>" /></td>


		</tr>
		<tr>
			<td><liferay-ui:message key="co-quan-yeu-cau" /></td>
			<td colspan="3"><input type="text" style="width: 96%" name="<portlet:namespace />coQuanYeuCauTrichLuc" id="<portlet:namespace />coQuanYeuCauTrichLuc" readonly="readonly" value="<%=coQuanYeuCauTrichLuc%>" /></td>
		</tr>
		<tr>
			<td><liferay-ui:message key="ho-va-ten-nguoi-can-trich-luc" /></td>
			<td colspan="3"><input type="text" style="width: 96%" name="<portlet:namespace />hoVaTenNguoiCanTrichLuc" id="<portlet:namespace />hoVaTenNguoiCanTrichLuc" readonly="readonly" value="<%=hoVaTenNguoiCanTrichLuc%>" /></td>
		</tr>
		<tr>
			<td><liferay-ui:message key="tinh-trang-an-tich" />&nbsp;<font color="red">*</font></td>
			<td colspan="3"><input  class="mandatory" message="<liferay-ui:message key='tinh-trang-an-tich' />" type="text" style="width: 96%" name="<portlet:namespace />tinhTrangAnTich" id="<portlet:namespace />tinhTrangAnTich" value="<%=tinhTrangAnTich%>" /></td>
		</tr>
		<tr>
			<td><liferay-ui:message key="so-ban-an-ngay-thang-nam-tuyen-an-toa-tuyen-an" /></td>
			<td colspan="3"><textarea style="height:70px" name="<portlet:namespace />thongTinAnTich" id="<portlet:namespace />thongTinAnTich"><%=thongTinAnTich %></textarea></td>
		</tr>
		<tr>
			<td><liferay-ui:message key="toi-danh-dieu-luat-ap-dung" /></td>
			<td colspan="3"><textarea style="height:70px" name="<portlet:namespace />toiDanh" id="<portlet:namespace />toiDanh"><%=toiDanh %></textarea></td>
		</tr>
		<tr>
			<td><liferay-ui:message key="hinh-phat-chinh" /></td>
			<td colspan="3"><textarea style="height:70px" name="<portlet:namespace />hinhPhatChinh" id="<portlet:namespace />hinhPhatChinh"><%=hinhPhatChinh %></textarea></td>
		</tr>
		<tr>
			<td><liferay-ui:message key="hinh-phat-bo-sung" /></td>
			<td colspan="3"><textarea style="height:70px" name="<portlet:namespace />hinhPhatBoSung" id="<portlet:namespace />hinhPhatBoSung"><%=hinhPhatBoSung %></textarea></td>
		</tr>
		<tr>
			<td><liferay-ui:message key="tinh-trang-thi-hanh-an" />&nbsp;<font color="red">*</font></td>
			<td colspan="3"><textarea style="height:70px"  class="mandatory" message="<liferay-ui:message key='tinh-trang-thi-hanh-an' />" name="<portlet:namespace />tinhTrangThiHanhAn" id="<portlet:namespace />tinhTrangThiHanhAn"><%=tinhTrangThiHanhAn %></textarea></td>
		</tr>
		<tr>
			<td><liferay-ui:message key="nguoi-tra-cuu" />&nbsp;<font color="red">*</font></td>
			<td><input type="text"  class="mandatory" message="<liferay-ui:message key='nguoi-tra-cuu' />" name="<portlet:namespace />nguoiTraCuu" id="<portlet:namespace />nguoiTraCuu" value="<%=nguoiTraCuu%>" /></td>
			<td><liferay-ui:message key="ngay-tra-cuu" />&nbsp;<font color="red">*</font></td>
			<td class="ip-datepicker"><input type="text"  class="mandatory" message="<liferay-ui:message key='ngay-tra-cuu' />" style="width: 90%" name="<portlet:namespace />ngayTraCuu" id="<portlet:namespace />ngayTraCuu" value="<%=ngayTraCuu%>" /></td>
		</tr>
		
		<tr>
			<td/></td>
			<td colspan="3">
				<div align="left">
					<input type="button" class="btn btn-primary" value="<liferay-ui:message key="luu-ket-qua-trich-luc" />" onclick="<portlet:namespace/>luuKetQuaTrichLuc()" />
					<input type="button" class="btn btn-primary" value="<liferay-ui:message key="in-ket-qua-trich-luc" />" onclick="<portlet:namespace/>inKetQuaTrichLuc()" />
					
					<portlet:renderURL var="troVeDanhSachTimKiemDoiTuongTrichLuc">
						<portlet:param name="mvcPath" value="/html/view.jsp" />
						<portlet:param name="chonChucNangItem" value="<%=TienAnConstants.QUAN_LY_YEU_CAU_TRICH_LUC%>" />
						<portlet:param name="tabs1" value="doi_tuong_trich_luc" />
						<portlet:param name="redirect" value="<%=redirect.toString()%>" />
						<portlet:param name="yeuCauTrichLucId" value="<%=String.valueOf(yeuCauTrichLucId)%>" />
					</portlet:renderURL>
					<input type="button" onclick="javascript:submitForm(document.hrefFm,'<%=troVeDanhSachTimKiemDoiTuongTrichLuc.toString() %>');" class="btn" value="<liferay-ui:message key="tro-ve" />" />
				</div>
			</td>
		</tr>
	</table>
</form>

